package com.example.myapplication;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.WindowInsetsAnimation;
import android.widget.Button;
import android.widget.TextView;

import androidx.activity.EdgeToEdge;
import androidx.activity.result.ActivityResult;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContract;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import java.net.URI;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_main);
        loadTokenFromManifests();


        Button jumpButton = findViewById(R.id.jumpButton);
        jumpButton.setOnClickListener(view->{
            // 跳转其他 activity
            Intent intent = new Intent();
            intent.setClass(MainActivity.this,CalculatorActivity.class);
            startActivity(intent);
        });

        findViewById(R.id.jumpToSecond).setOnClickListener(view->{
            Intent intent = new Intent(this, ButtonActivity.class);

            // 栈中存在待跳转的 activity 时，则重新创建该实例，
            // 并清除原实例上方的所有实例
            // 运行之后发现：两个活动多次来回跳转，返回的时候只会每个页面返回一次
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

            startActivity(intent);
        });

        findViewById(R.id.login).setOnClickListener(view->{

            Intent intent = new Intent(this, GridLayoutActivity.class);

            Bundle extras = new Bundle();
            extras.putString("param1","传个参数给你吧");
            intent.putExtras(extras);

            /*
            * 因为登录成功之后不再返回登录页面，所以要把当前栈清空
            * 但是栈被清空之后就再也用不了了，所以需要创建一个新的活动栈
            * */
            intent.setFlags(
                    Intent.FLAG_ACTIVITY_CLEAR_TASK
            |Intent.FLAG_ACTIVITY_NEW_TASK);

            startActivity(intent);
        });

        findViewById(R.id.dialog).setOnClickListener(view->{

            Intent intent = new Intent();
            // 打电话
            intent.setAction(Intent.ACTION_DIAL);

            intent.setData( Uri.parse("tel:13211634008"));

            startActivity(intent);
        });

        findViewById(R.id.sendMessage).setOnClickListener(view->{

            Intent intent = new Intent();
            intent.setAction(Intent.ACTION_SENDTO);
            intent.setData(Uri.parse("smsto:13211634008"));
            startActivity(intent);
        });

        findViewById(R.id.sendDataToActivityByIntent).setOnClickListener(view->{

            Intent intent = new Intent(this, GridLayoutActivity.class);

            Bundle bundle = new Bundle();

            bundle.putLong("id",123L);
            bundle.putString("tips","要把这个数据显示出来哦");
            intent.putExtras(bundle);

            startActivity(intent);
        });

        TextView returnedDataView = findViewById(R.id.returnedData);

        ActivityResultLauncher<Intent> intentActivityResultLauncher = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), (ActivityResult result) -> {
            // 从子 Activity 返回时，会被调用
            if (result.getResultCode()== Activity.RESULT_OK) {
                Intent data = result.getData();
                if (data!=null) {

                    String resultData = data.getStringExtra("result");
                    returnedDataView.setText(resultData);
                }
            }
        });

        findViewById(R.id.getResultFromNewActivity).setOnClickListener(view->{

            Intent intent = new Intent(this, GravityActivity.class);

            Bundle extras = new Bundle();
            extras.putString("param1","传入参数");
            intent.putExtras(extras);

            //startActivityForResult(intent,10); 不推荐
            intentActivityResultLauncher.launch(intent);
        });
    }

    /**
     * 从 AndroidManifest.xml 中加载元数据
     */
    private void loadTokenFromManifests() {
        TextView headView = findViewById(R.id.head);

        try {
            ActivityInfo activityInfo = getPackageManager().getActivityInfo(getComponentName(), PackageManager.GET_META_DATA);

            String token = activityInfo.metaData.getString("token");
            headView.setText(token);
        } catch (PackageManager.NameNotFoundException e) {
            throw new RuntimeException(e);
        }
    }
}